
//添加编制车辆
var setHtml=`<div class="layui-form-item set">
<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="设备名称" autocomplete="off" class="layui-input setname" readonly>
        </div>
        <div class="layui-form-mid">-</div>
        <div class="layui-input-inline" style="width: 100px;">
            <input type="text"  placeholder="用途" autocomplete="off" class="layui-input setuse" readonly>
        </div>
    </div>
</div>
</div> `;
var id ='{$Think.get.id}'
$('.backs').on('click',()=>{
    console.log(123)
    window.location.href='{:url("html/car/car_compile")}'
})
// 循环遍历
if(id){
    Load();
	ajax(api.compile.list,{
		where_json:JSON.stringify({
			id:['in',"{$Think.get.id}"]
		})
	},'post').done(function(res) {
        // 车辆选择
        $('.number').on('click', () => {
            // opsearch(`{:url("html/car/car")}?op=search&company_id=${res.data[0]['company'].company_id}&company_strength_id=0`)
            opsearch(`{:url("html/car/car")}?op=search`)
        })
        console.log(res)
        Load(1);
		for(var key in res.data[0]){
            if(key == 'company'){
                $('input[name=company_id]').val(res.data[0]['company'].company_id)
                $('input[name=company_name]').val(res.data[0]['company'].company_name)
            }else if(key == 'strength_type'){
                if(res.data[0][key]==1){
                    $('.strength_type_name').val('执法执勤用车')
                   
                }else if(res.data[0][key]==2){
                    $('.strength_type_name').val('特种专业技术用车')
                   
                }else if(res.data[0][key]==3){
                    $('.strength_type_name').val('机要通信用车')
                  
                }else if(res.data[0][key]==4){
                    $('.strength_type_name').val('应急保障用车')
                 
                }
                
            }else if(key == 'approval_document'){
                if(res.data[0][key]){
                    var imgData = JSON.parse(res.data[0][key])
                    if(imgData.approval_img){
                        $('#img1').attr('src',domain+imgData.approval_img)
                    }else{
                        $('#img1').attr('src','/assets/img/noimg.gif')
                    }
                    if(imgData.budget_img){
                        $('#img2').attr('src',domain+imgData.budget_img)
                    }else{
                        $('#img2').attr('src','/assets/img/noimg.gif')
                    }               
                }else{
                    $('#img1').attr('src','/assets/img/noimg.gif')
                    $('#img2').attr('src','/assets/img/noimg.gif')
                }
            }else if(key =='standard'){
                let standardData= JSON.parse(res.data[0][key]);

                for(keys in standardData){
                    if(keys =='set'){
                for(var i =0;i<standardData[keys].length;i++){
                    if(i>0){
                       $('.set').after(setHtml) 
                    }
                    $('.setname').eq(i).val(standardData[keys][i].setname)
                    $('.setuse').eq(i).val(standardData[keys][i].setuse)
                }
                    }else if(keys == 'new_energy'){
                        if(standardData[keys]==1){
                            console.log(1)
                            $(`.${keys}`).attr('checked',true)
                        }else{
                            $(`.${keys}`).attr('checked',false)
                        }

                    }else if(keys =='car_type'){
                        if(standardData[keys]==1){
                            $('.car_type_name').val('大型客车')
                           
                        }else if(standardData[keys]==2){
                            $('.car_type_name').val('中型客车')
                           
                        }else if(standardData[keys]==3){
                            $('.car_type_name').val('小型客车')
                          
                        }
                    
                }else{
                        $(`.${keys}`).val(standardData[keys])
                    }
                }

                for(keys in standardData){
                    if(keys == 'set'){                       
                    }else{
                        if(standardData.keys){
                            $(`.${keys}`).val(standardData[keys])
                        }else{
                            $(`.${keys}`).attr('placeholder','未填写')  
                        }
                        
                    }
                   
                }
                
            }else{
                if(res.data[0][key]){
                 $('input[name='+key+']').val(res.data[0][key])
                }else{
                    $('input[name='+key+']').attr('placeholder','未填写')                  
                }
            }
         
            
        }
        form.render()
	}).fail(function(err) {
		Err('请求失败')
    });
}else{
    Err('暂无数据')
}

$('.pass').on('click',()=>{
    var reason =''
    form.on('submit(save)', function(data){
        console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
         reason = data.field.reason;
         return false; 
    })
   var Tip= layer.confirm('是否确定审核通过', {
        btn: ['确定','取消'] //按钮
      }, function(){
        ajax(api.compile.audit,{id:id,state:2,reason:reason},'post').done(function(res) {
            if(res.code ==1){
                layer.msg('审核通过', {icon: 1});
                window.location.href= "{:url('html/car/car_compile')}"
            }else{
                layer.msg('审核失败,稍后再试', {icon: 2});
            }
        
        }).fail((err)=>{
            layer.msg('请求失败,稍后再试', {icon: 0});
        })
      }, function(){
       layer.close(Tip)
      });
})

$('.bind').on('click',()=>{
    form.on('submit(save)', function(data){
        console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
        var Tip= layer.confirm('是否确定绑定该车辆', {
            btn: ['确定','取消'] //按钮
          }, function(){
              var datas={};
                datas.car_id =data.field.car_id;
                datas.id =id;
                   ajax(api.compile.bind,datas,'post').done((res)=>{
                        if(res.code == 1){
                            layer.msg('绑定成功', {icon: 1});
                            window.location.href= "{:url('html/car/car_compile')}"
                        }else{
                            Err(res.msg)
                        }
                   }).fail(()=>{
                    Err('请求失败')
                   })
            
          }, function(){
           layer.close(Tip)
          });
         return false; 
    })
})



